package io;

import java.io.*;

/**
 * 序列化
 */
public class Serializable_ implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name = "name";
    private int age = 18;

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("my.out"));
        //输出流保存的文件名为
        // ObjectOutputStream能把Object输出成Byte流
        oos.writeObject(new Serializable_());
        oos.flush();
        oos.close();
        fan();
    }

    //反序列的过程
    private static void fan() throws IOException, ClassNotFoundException {
        ObjectInputStream oin = new ObjectInputStream(new FileInputStream("my.out"));
        Serializable_ mts = (Serializable_) oin.readObject();//由Object对象向下转型为MyTest对象
        System.out.println("name=" + mts.name);
        System.out.println("age=" + mts.age);
    }

}
